<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>电机日志</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css"/>
    <style>
        .layui-form-label {
            width: 60px;
        }
        .layui-inline {
            vertical-align: top;
            margin-right: 0px;
        }
        .layui-form-item .pear-btn{
            height: 38px;
            line-height: 38px;
        }
    </style>
</head>
<body class="pear-container">
<div class="layui-card">
    <div class="layui-card-body">
        <div class="layui-row layui-col-space15">
            <div class="layui-col-md2">
                <button type="button" class="pear-btn layui-btn-fluid  layui-btn-primary layui-border-black"
                        id="clearLog">
                    <!--                    <i class="layui-icon layui-icon-add-circle" style="font-size: 17px"></i>-->
                    <img src="/img/删除.png" alt="" style="height: 18px;">
                    清除日志
                </button>
            </div>
        </div>
    </div>
</div>
<div class="layui-card">
    <div class="layui-card-body">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">档案名字</label>
                    <div class="layui-input-inline">
                        <input type="text" name="fileName" placeholder="" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">类型</label>
                    <div class="layui-input-inline">
                        <select name="borrowingType" lay-search>
                            <option value="">请选择机柜编号</option>
                            <option value="1">借出</option>
                            <option value="2">归还</option>
                            <option value="3">异常</option>
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">选择机柜</label>
                    <div class="layui-input-inline">
                        <select name="number" lay-search lay-filter="cabinetChange">
                            <option value="">请选择机柜编号</option>
                            <option th:each="dict:${hostList}" th:value="${dict.number}" th:text="${dict.number}">
                        </select>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <label class="layui-form-label">时间</label>
                    <div class="layui-inline" id="test6">
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="startDate" id="startDate" class="layui-input"
                                   placeholder="开始日期">
                        </div>
                        <div class="layui-form-mid">-</div>
                        <div class="layui-input-inline">
                            <input type="text" autocomplete="off" name="endDate" id="endDate" class="layui-input"
                                   placeholder="结束日期">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-inline">
                    <button class="pear-btn pear-btn-md pear-btn-primary" lay-submit lay-filter="submitFilter">
                        <i class="layui-icon layui-icon-search"></i>
                        查询
                    </button>
                    <button type="reset" class="pear-btn pear-btn-md">
                        <i class="layui-icon layui-icon-refresh"></i>
                        重置
                    </button>
                </div>
            </div>
        </form>
        <div class="layui-tab layui-tab-card">
            <ul class="layui-tab-title">
                <li class="layui-this">操作日志</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <table style="margin-top: 10px;" id="file-log-table" lay-filter="file-log-table"></table>
                </div>
            </div>
        </div>
    </div>
</div>
</body>

<script type="text/html" id="log-bar">
    {{#if (d.success == true) { }}
    <button class="pear-btn pear-btn-sm pear-btn-success" lay-event="edit"><i class="layui-icon layui-icon-ok"></i>
        成功
    </button>
    {{# }else if(d.success == false){ }}
    <button class="pear-btn pear-btn-sm pear-btn-danger" lay-event="error"><i class="layui-icon layui-icon-help"></i>
        异常
    </button>
    {{# } }}
</script>

<script type="text/html" id="log-createTime">
    {{layui.util.toDateString(d.createTime,  "yyyy-MM-dd HH:mm:ss")}}
</script>
<!-- 推荐 -->
<script type="text/html" id="TPL-dropdpwn-demo">
    <button class="pear-btn pear-btn-primary pear-btn-sm"
            style="width: 105px;background-color: #16baaa !important;border: 1px solid #16baaa;"
            lay-event="viewPicture">查看图片
    </button>
</script>

<script src="/component/layui/layui.js"></script>
<script src="/component/pear/pear.js"></script>
<script>
    layui.use(['table', 'form', 'jquery', 'element', 'laydate'], function () {
        let table = layui.table;
        let form = layui.form;
        let element = layui.element;
        let laydate = layui.laydate;
        let $ = layui.jquery;

        $("#clearLog").click(function () {
            var str = '<div><span  style="color: red">该操作会删除所有日志，请谨慎操作？</span></div>';
            layer.confirm(str, {
                icon: 0,
                title: '警告'
            }, function (index) {
                layer.close(index);
                let loading = layer.load();
                $.ajax({
                    url: "/file/log/clear",
                    dataType: 'json',
                    type: 'POST',
                    contentType: "application/json;charset=utf-8",
                    data: JSON.stringify(),
                    success: function (result) {
                        layer.close(loading);
                        if (result.code == 200) {
                            layer.msg(result.message, {
                                icon: 1,
                                time: 3000
                            }, function () {
                                table.reload('fileLogTable');
                            });
                        } else {
                            layer.msg(result.message, {
                                icon: 2,
                                time: 3000
                            });
                        }
                    }
                })
            });
        })

        // 设置最大可选的日期
        function maxDate() {
            var now = new Date();
            return now.getFullYear() + "-" + (now.getMonth() + 1) + "-" + now.getDate();
        }

        //日期范围
        laydate.render({
            elem: '#test6',
            max: maxDate(),
            //设置开始日期、日期日期的 input 选择器
            //数组格式为 2.6.6 开始新增，之前版本直接配置 true 或任意分割字符即可
            range: ['#startDate', '#endDate'],
            theme: '#000000'
        });

        let cols = [
            [
                {title: '机柜编号', field: 'number', align: 'center'},
                {title: '档案名', field: 'file_name', align: 'center'},
                {title: '位置编号', field: 'file_position', align: 'center'},
                {title: '用户名', field: 'user_name', align: 'center'},
                {
                    title: '操作类型', field: 'borrowing_type', align: 'center', templet: function (d) {
                        if (d.borrowing_type == 1) {
                            return '<span style="color: #0895ff;">借出</span>';
                        } else if (d.borrowing_type == 2) {
                            return '<span style="color: #f56c6c;">归还</span>'
                        } else if (d.borrowing_type == 3) {
                            return '<span style="color: #e6a23c;">异常</span>'
                        }
                    }
                },
                {title: '存取时间', field: 'create_time', align: 'center'},

            ]
        ]
        table.render({
            elem: '#file-log-table',
            url: '/file/log/list',
            page: true,
            cols: cols,
            skin: 'line',
            toolbar: true,
            defaultToolbar: [{
                title: '刷新',
                layEvent: 'refresh',
                icon: 'layui-icon-refresh',
            }, 'filter', 'print', 'exports'],
            id: "fileLogTable"
        });
        // 刷新按钮
        window.refresh = function (param) {
            table.reload('fileLogTable');
        }
        table.on('toolbar(file-log-table)', function (obj) {
            if (obj.event === 'refresh') {
                window.refresh();
            }
        });
        table.on('tool(file-log-table)', function (obj) {
            if (obj.event === 'viewPicture') {
                window.viewPicture(obj);
            }
        })

        //查看用户图片
        window.viewPicture = function (obj) {
            layer.open({
                title: '用户照片',
                type: 2,
                shade: 0.1,
                area: ['500px', '400px'],
                content: '/file/log/user/img?id=' + obj.data.id
            });

        }

        form.on('submit(submitFilter)', function (data) {
            table.reload('fileLogTable', {where: data.field})
            return false;
        });
    })
</script>
</html>